package com.example.onlinetaxi.view.Adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.ReceivedOrderCallBack;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;

import java.util.List;

public class DoingOrderAdapter extends RecyclerView.Adapter<DoingOrderAdapter.ViewHolder> {

    // 用于构造页面的订单列表
    private List<ReceivedOrderDto> doingOrderList;
    // 点击事件回调函数
    private ReceivedOrderCallBack receivedOrderCallBack;

    public static Drawable drawable;
    public static Drawable drawable2;

    // 构造函数
    public DoingOrderAdapter(List<ReceivedOrderDto> doingOrderList) {
        this.doingOrderList = doingOrderList;
    }

    /**
     * 内部类也继承其内部类，并重写构造器
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout doingOrderAllInfo;
        TextView doingOrderId;
        // 订单类型（实时单/预约单）
        TextView doingOrderLeftColor;
        TextView doingOrderUseCarType;
        TextView doingOrderStartInfo;
        TextView doingOrderEndInfo;
        TextView doingOrderCarBrand;
        TextView doingOrderCarColor;
        TextView doingOrderCarNumber;
        TextView doingOrderInfo;
        TextView doingOrderInfo2;
        Button cancelButton;

        public ViewHolder(View view) {
            super(view);
            // 布局
            doingOrderAllInfo = view.findViewById(R.id.doing_order_all_info);  // 点击后进入司机实时位置界面
            // 按钮
            cancelButton = view.findViewById(R.id.doing_order_cancel_btn);
            // 文本框
            doingOrderLeftColor = view.findViewById(R.id.doing_order_left_color);
            doingOrderId = view.findViewById(R.id.doing_order_id);
            doingOrderUseCarType = view.findViewById(R.id.doing_order_use_car_type);
            doingOrderStartInfo = view.findViewById(R.id.doing_order_start_info);
            doingOrderEndInfo = view.findViewById(R.id.doing_order_end_info);
            doingOrderCarBrand = view.findViewById(R.id.doing_order_car_brand);
            doingOrderCarColor = view.findViewById(R.id.doing_order_car_color);
            doingOrderCarNumber = view.findViewById(R.id.doing_order_car_number);
            doingOrderInfo = view.findViewById(R.id.doing_order_info);
            doingOrderInfo2 = view.findViewById(R.id.doing_order_info2);
        }
    }

    // 第二个被回调，创建一个ViewHolder对象返回
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_doing, parent, false);
        // 布局传递到内部类的构造函数中
        ViewHolder holder = new ViewHolder(view);
        // 返回内部类对象
        return holder;
    }

    // 子项数据赋值，通过这个方法实现滑动添加，第三个被回调
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 得到当前order
        ReceivedOrderDto order = doingOrderList.get(position);

        // 放入本地存储
        if (!order.getId().toString().equals("")) {
            LocalStorage.currentOrderId = order.getId().toString();
        } else {
            LocalStorage.currentOrderId = null;
        }

        holder.doingOrderAllInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用回调函数跳转等待接单
                receivedOrderCallBack.jump(order);
            }
        });
        holder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用回调函数发送请求，删除当前订单
                receivedOrderCallBack.clickButton(order);
            }
        });

        // 常规控件内容
        holder.doingOrderId.setText("订单id:" + order.getId());
        holder.doingOrderUseCarType.setText(order.getUseCarType());
        holder.doingOrderStartInfo.setText(order.getStartInfo());
        holder.doingOrderEndInfo.setText(order.getEndInfo());
        holder.doingOrderCarBrand.setText("车辆品牌:" + order.getCarBrand());
        holder.doingOrderCarColor.setText("车辆颜色:" + order.getCarColor());
        holder.doingOrderCarNumber.setText("车牌号:" + order.getCarNumber());

        // 设置订单id
        if (order.getCancelState().equals("1") && order.getState().equals("3")) {
            // 等待撤销状态
            if (order.getCancelReason().equals("")) {
                order.setCancelReason("无");
            }

            holder.doingOrderInfo.setText("订单撤销原因:" + order.getCancelReason());
            holder.doingOrderInfo2.setText("撤销中");
            holder.doingOrderInfo.setTextColor(Color.parseColor("#e85d30"));
            holder.doingOrderInfo2.setTextColor(Color.parseColor("#e85d30"));
            holder.doingOrderLeftColor.setBackground(drawable);
            // 按钮不可见且不占用空间
            holder.cancelButton.setVisibility(View.GONE);

        }

        // 等待接单状态
        if (order.getCancelState().equals("0") && order.getState().equals("3")) {
            holder.doingOrderInfo.setText("司机师傅正火速前往起点~");
            holder.doingOrderInfo2.setText("等待接车");
            holder.doingOrderInfo.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderInfo2.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderLeftColor.setBackground(drawable2);
            // 设置按钮可见
            holder.cancelButton.setVisibility(View.VISIBLE);
        }

        // 撤销订单被拒绝
        if (order.getCancelState().equals("3") && order.getState().equals("3")) {
            holder.doingOrderInfo.setText("司机师傅正火速前往起点~");
            holder.doingOrderInfo2.setText("等待接车\n撤销请求被拒绝");
            holder.doingOrderInfo2.setTextSize(12);
            holder.doingOrderInfo.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderInfo2.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderLeftColor.setBackground(drawable2);
            // 设置按钮可见
            holder.cancelButton.setVisibility(View.VISIBLE);
        }

        // 成功撤销订单，应该出现在已取消
        if (order.getCancelState().equals("2") && order.getState().equals("3")) {
        }

        if (order.getState().equals("4")) {
            // 前往终点状态
            holder.doingOrderInfo.setText("司机师傅正火速送您至终点~");
            holder.doingOrderInfo2.setText("前往终点");
            holder.doingOrderInfo.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderInfo2.setTextColor(Color.parseColor("#2953ff"));
            holder.doingOrderLeftColor.setBackground(drawable2);
            // 按钮不可见且不占用空间
            holder.cancelButton.setVisibility(View.GONE);

        }
    }

    //  @return 获取长度，第一个被回调
    public int getItemCount() {
        return doingOrderList.size();
    }

    // 设置回调函数
    public void setReceivedOrderCallBack(ReceivedOrderCallBack receivedOrderCallBack) {
        this.receivedOrderCallBack = receivedOrderCallBack;
    }
}
